package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.ReturnStatus;
import com.ygqh.baby.ao.ReturnType;
import com.ygqh.baby.po.YgOrder;
import com.ygqh.baby.po.YgReturnOrder;
import com.ygqh.baby.service.RepairBalanceDataService;
import com.ygqh.baby.service.YgOrderService;
import com.ygqh.baby.service.YgReturnOrderService;
import com.ygqh.baby.service.task.YgBalanceTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 〈返利修复〉
 *
 * @author guohao
 * @date 2018/12/14 10:56
 * @since 1.0.0
 */
@Service
public class RepairBalanceDataServiceImpl implements RepairBalanceDataService {

    @Autowired
    private YgBalanceTaskService ygBalanceTaskService;
    @Autowired
    private YgOrderService ygOrderService;
    @Autowired
    private YgReturnOrderService ygReturnOrderService;

    @Override
    public void repairPreBalanceData(Long orderId) {
        YgOrder order = ygOrderService.findOrderDetailByOrderId(orderId);
        List<YgReturnOrder> returnOrderList = ygReturnOrderService.findRetrunByOrderId(orderId);
        returnOrderList = returnOrderList.stream().filter(ro -> ReturnType.Return.equals(ro.getReturnType()) && ReturnStatus.Complete.equals(ro.getReturnStatus()))
                .collect(Collectors.toList());
        List<YgOrder> orderList = Collections.singletonList(order);
        ygBalanceTaskService.doPreBalanceTask(orderList, returnOrderList);
    }

    @Override
    public void repairPreBalanceData(Long[] orderIds, String[] returnCodes) {
        List<YgOrder> orderList = null;
        if (orderIds != null && orderIds.length > 0) {
            orderList = ygOrderService.findOrderDetailByOrderNoList(Arrays.asList(orderIds));
        }
        List<YgReturnOrder> returnOrderList = null;
        if (returnCodes != null && returnCodes.length > 0) {
            returnOrderList = ygReturnOrderService.findDetailByReturnCodeList(Arrays.asList(returnCodes));
        }
        ygBalanceTaskService.doPreBalanceTask(orderList, returnOrderList);
    }
}
